package org.mclll.xwangplugin.enchantment.config;

import com.google.common.collect.Maps;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;

import java.io.File;
import java.util.Map;
import java.util.Set;

/**
 * @author 洛佑希
 */
public class EnchantmentLimitConfig {
    /*限制附魔配置文件名*/
    public static final String CONFIG_FILE_NAME = "enchantmentLimit.yml";

    /*被限制的附魔列表*/
    public static Map<Enchantment, Integer> limitEnchantments;

    /*初始化*/
    public static void reloadConfig(File file) {
        if (limitEnchantments == null) {
            limitEnchantments = Maps.newHashMap();
        } else {
            limitEnchantments.clear();
        }
        /*读取配置文件*/
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(file, CONFIG_FILE_NAME));
        /*获取配置文件限制列表*/
        ConfigurationSection section = config.getConfigurationSection("Limits");
        Set<String> limitList = null;
        if (section != null) {
            limitList = section.getKeys(false);
        }
        /*遍历配置文件限制列表 存入内存*/
        if (limitList != null) {
            for (String enchantmentName : limitList) {
                Enchantment enchantment;
                enchantment = Enchantment.getByKey(NamespacedKey.fromString(enchantmentName));
                Integer maxLevel = section.getInt(enchantmentName);
                limitEnchantments.put(enchantment, maxLevel);
            }
        }
    }
}
